# -*- coding: utf-8 -*-
# @Time    : 2019/2/25 16:50
# @Author  : Macher
# @File    : mx.py
# @Software: PyCharm Community Edition

# coding=utf-8
import json
import requests
from bs4 import BeautifulSoup
import  mysql.connector
import time

config={'host':'219.216.65.41',
        'user':'root',
        'password':'123456',
        'port':3306 ,
        'database':'mydata',
        'charset':'utf8'}



def updataSql(sql,s):          #插入数据库的函数
    conn = mysql.connector.connect(**config)
    cursor = conn.cursor()
    cursor.execute(sql,s)
    conn.commit()
    cursor.close()
    conn.close()

def getip():          #插入数据库的函数
    conn = mysql.connector.connect(**config)
    cursor = conn.cursor()
    # sql = 'SELECT pid,url FROM phish WHERE ip = "" limit 10000'
    sql = "SELECT ip FROM experiment_date WHERE kind = 'whitelist' and mv is null "
    cursor.execute(sql)
    results = cursor.fetchall()
    new = []
    for row in results:
        new.append(row[0])
    new = list(set(new))
    cursor.close()
    return new

def mxtool(info):
    dict1 = {'blacklist':''}
    url = "https://mxtoolbox.com/Public/Lookup.aspx/DoLookup2"
    headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
               'Accept-Encoding': 'gzip, deflate, br',
               'Accept-Language':'zh-CN,zh;q=0.8',
               'Connection': 'keep-alive',
               'Content-Type':'application/json; charset=UTF-8',
               'Cookie': 'MxVisitorUID=d797041e-bdc6-462c-a41c-4023d4d8fc74; _vis_opt_exp_312_exclude=1; _vwo_uuid=974491D5B9066661CAAE036DBFB5984E; _vis_opt_exp_335_combi=2; _vis_opt_s=2%7C; _ceg.s=ov46sb; _ceg.u=ov46sb; _gat=1; ismobile=false; _cio=aa8504a9-17c9-86fd-7dfb-eb0cbc5df5a4; _ga=GA1.2.636406067.1501461605; _gid=GA1.2.21041308.1507970087; ki_t=1501461610219%3B1507970091131%3B1507970111250%3B12%3B81; ki_r=; _vwo_uuid_v2=974491D5B9066661CAAE036DBFB5984E|782ec007ad63acefe56b46a7f7b32ad7; _mx_u={"UserId":"00000000-0000-0000-0000-000000000000","UserName":null,"FirstName":null,"IsAdmin":false,"IsPaidUser":false,"IsLoggedIn":false,"MxVisitorUid":"d797041e-bdc6-462c-a41c-4023d4d8fc74","AppUID":"2eab821e-fdad-4545-9730-d2ce7e6ccb3e"}; _gaexp=GAX1.2.jQWr1JWLTCOH_ZPj74b2mA.17514.1!3jEbJbKJQM6-_lqmJEDxKw.17517.2!fUhZDJh2SvymWf4W_nFisQ.17544.1; _mx_vtc=AB-177=Variation&AB-175=Variation&AB-197B=Variation&AB-166=Variation&AB-205=Control&VWO-Blocked=true&AB-226=Control&AB-240=Control&AB-216=Control&AB-229=variation&AB-230=Control',
                'Host': 'mxtoolbox.com',
               'MasterTempAuthorization':'2eab821e-fdad-4545-9730-d2ce7e6ccb3e',
               'Origin':'https://mxtoolbox.com',
               'Referer':'https://mxtoolbox.com/SuperTool.aspx?action=blacklist%3a98.126.12.45&run=toolpage',
               'X-Requested-With': 'XMLHttpRequest',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
    proxies = {
        "http": None,
        "https": None,
    }

    r = requests.post(url,data=json.dumps({'inputText': "blacklist:"+info, 'resultIndex': 4}),headers = headers ,proxies = proxies)

    mx =str(r.text)
    mx = json.loads(mx)
    m1 =  mx['d'].replace('{','').replace('}','').replace('\u003e','>').replace('\u003c','<').replace('\u0027',"'").replace('\u0026','&').replace('\\','')
    #print m1
    bl = []
    result = ''
    soup = BeautifulSoup(m1, 'lxml')
    strin1 = soup.find(class_="tool-result-div")
    # print (strin1)
    try:
        for strin2 in strin1.find_all(attrs={'src': 'https://mxtoolbox.com/public/images/statusicons/problem.png'}):
            if strin2 is not None:
                p = strin1.find(class_="tool-result-body")
                re = strin2.next_element.next_element.contents[0].string
                if re not in bl:
                    bl.append(re)

        result= tuple({''.join(bl)})
        ip = tuple({str(info)})
        sql = 'update experiment_date set mv = %s where ip = %s'
        print(result + ip)
        updataSql(sql,result + ip)
    except:pass

l1 = getip()
for i in l1:
    print(i)
    mxtool(i)
    time.sleep(15)
